package com.artisan.common.utils;

import com.artisan.common.pojo.ApiRes;
import com.artisan.common.pojo.AppApiBaseModel;

import javax.servlet.http.HttpServletRequest;
import java.io.Writer;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by on 2016/4/12.
 * 工具类
 */
public class Utils {

    /**
     * xierongxian
     * 生成四位随机不重复数
     * */
    public static String GetRandomNumber() {
        // 使用SET以此保证写入的数据不重复
        Set<Integer> set = new HashSet<Integer>();
        // 随机数
        Random random = new Random();

        while (set.size() < 4) {
            // nextInt返回一个伪随机数，它是取自此随机数生成器序列的、在 0（包括）
            // 和指定值（不包括）之间均匀分布的 int 值。
            set.add(random.nextInt(10));
        }
        Set<Integer> setstr = set;
        // 使用迭代器
        Iterator<Integer> iterator = setstr.iterator();
        // 临时记录数据
        String temp = "";
        while (iterator.hasNext()) {
            temp += iterator.next();
            // System.out.print(iterator.next());
        }
        return temp;
    }

    public static String getRandomNumber(int length) {
        // 使用SET以此保证写入的数据不重复
        Set<Integer> set = new HashSet<Integer>();
        // 随机数
        Random random = new Random();

        while (set.size() < length) {
            // nextInt返回一个伪随机数，它是取自此随机数生成器序列的、在 0（包括）
            // 和指定值（不包括）之间均匀分布的 int 值。
            set.add(random.nextInt(10));
        }
        Set<Integer> setstr = set;
        // 使用迭代器
        Iterator<Integer> iterator = setstr.iterator();
        // 临时记录数据
        String temp = "";
        while (iterator.hasNext()) {
            temp += iterator.next();
            // System.out.print(iterator.next());
        }
        return temp;
    }

    /**
     * 获取真实IP地址
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        System.out.println(ip);
        return ip;
//        return "123.57.153.142";
    }


    public static String getOutTradeNo() {
        String key = formatTime2MMddHHmmss()  + getRandom();
        return key.length() > 14 ? key.substring(0, 14) : key;
    }
    public static String formatTime2MMddHHmmss()
    {
        return new SimpleDateFormat("MMddHHmmss").format(System.currentTimeMillis());
    }


    public static String getRandom()
    {
        return String.valueOf(Math.random() * 9000 + 1000);
    }


    public static void main(String[] args) {
        for (int i=0;i<1000;i++) {
            System.out.println(getOutTradeNo());
        }
    }


    /**
     * 将分为单位的转换为元 （除100）
     *
     * @param amount
     * @return
     * @throws Exception
     */
    public static String changeF2Y(String amount){
        String CURRENCY_FEN_REGEX = "\\-?[0-9]+";
        if(amount.matches(CURRENCY_FEN_REGEX)) {
            return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString();
        }
        return "0.00";
    }

    public static AppApiBaseModel outTimeError(AppApiBaseModel appApiBaseModel, ApiRes apiRes){
        apiRes.setCode(ApiRes.RESERVE_MORE_CODE);
        apiRes.setDesc("用户信息失效，请重新登录");
        appApiBaseModel.setRes(apiRes);
        return appApiBaseModel;
    }

    public static AppApiBaseModel dataFomatError(AppApiBaseModel appApiBaseModel, ApiRes apiRes){
        apiRes.setCode(ApiRes.ERROR_FORMAT_CODE);
        apiRes.setDesc("请求参数格式不正确");
        appApiBaseModel.setRes(apiRes);
        return appApiBaseModel;
    }

    public static AppApiBaseModel dataIllegalError(AppApiBaseModel appApiBaseModel, ApiRes apiRes){
        apiRes.setCode(ApiRes.ILLEGAL_CUS_CODE);
        apiRes.setDesc("非法操作");
        appApiBaseModel.setRes(apiRes);
        return appApiBaseModel;
    }
}
